/**
 * @(#)Example.java, Nov 20, 2012. 
 * 
 * Copyright 2012 Yodao, Inc. All rights reserved.
 * YODAO PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.buaaer.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;


/**
 *
 * @author wangheng
 *
 */
public class Example {

    public static void main(String[] args) throws Exception {
        
        ServerSocket server = new ServerSocket(9876);

        Socket socket = server.accept();
        InputStream input = socket.getInputStream();
        OutputStream output = socket.getOutputStream();
        Thread t1 = new Thread(new Example().new InputThread(input));
        Thread t2 = new Thread(new Example().new OutputThread(output));
        
        t1.start();
        t2.start();
        
        t1.join();
        t2.join();
    }
    
    class OutputThread implements Runnable {
        
        OutputStream output;
        
        OutputThread(OutputStream output) {
            this.output = output;
        }
        
        @Override
        public void run() {
            OutputStreamWriter writer = new OutputStreamWriter(output);
            while (true) {
                
                BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
                try {
                    String str = reader.readLine();
                    System.out.println("str : " + str);
                    if (str.equals("end")) {
                        break;
                    }
                    writer.write(str+"\n");
                    writer.flush();
                    
                    
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        
    }
    
    class InputThread implements Runnable {

        InputStream input;
        
        InputThread(InputStream input) {
            this.input = input;
        }
        
        @Override
        public void run() {
            
            while (true) {
                try {
                    StringBuilder sb = new StringBuilder();
                    while (true) {
                        int c = input.read();
                        sb.append((char) c);
                        if (c == -1 || c == '\n') {
                            break;
                        }

                    }
                    System.out.println(sb.toString());
                    if (sb.toString().equals("end\r\n")) {
                        input.close();
                        break;
                    }
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            
                
        }
        
    }

}
